package com.zp.drug.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.DrugExportDetail;
import com.zp.drug.entity.bo.DrugExportDetailBo;
import com.zp.drug.entity.vo.DrugExportDetailVo;
import com.zp.drug.mapper.DrugExportDetailMapper;
import com.zp.drug.service.IDrugExportDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 药品出库明细Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-05-23
 */
@RequiredArgsConstructor
@Service
public class DrugExportDetailServiceImpl implements IDrugExportDetailService {

    private final DrugExportDetailMapper baseMapper;

    /**
     * 查询药品出库明细
     */
    @Override
    public DrugExportDetailVo queryById(String detailId) {
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询药品出库明细列表(分页)
     */
    @Override
    public TableDataInfo<DrugExportDetailVo> queryPageList(DrugExportDetailBo bo, PageQuery pageQuery) {
        Page<DrugExportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药品出库明细列表(不分页)
     */
    @Override
    public List<DrugExportDetailVo> queryList(DrugExportDetailBo bo) {
        LambdaQueryWrapper<DrugExportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DrugExportDetail> buildQueryWrapper(DrugExportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DrugExportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMasterId()), DrugExportDetail::getMasterId, bo.getMasterId());
        lqw.eq(StringUtils.isNotBlank(bo.getStockId()), DrugExportDetail::getStockId, bo.getStockId());
        lqw.eq(bo.getItemNo() != null, DrugExportDetail::getItemNo, bo.getItemNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugInfoId()), DrugExportDetail::getDrugInfoId, bo.getDrugInfoId());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugCode()), DrugExportDetail::getDrugCode, bo.getDrugCode());
        lqw.eq(bo.getPackageNum() != null, DrugExportDetail::getPackageNum, bo.getPackageNum());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageSpec()), DrugExportDetail::getPackageSpec, bo.getPackageSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageUnits()), DrugExportDetail::getPackageUnits, bo.getPackageUnits());
        lqw.eq(StringUtils.isNotBlank(bo.getMinSpec()), DrugExportDetail::getMinSpec, bo.getMinSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getMinUnits()), DrugExportDetail::getMinUnits, bo.getMinUnits());
        lqw.eq(StringUtils.isNotBlank(bo.getPackSize()), DrugExportDetail::getPackSize, bo.getPackSize());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), DrugExportDetail::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getProduceDate() != null, DrugExportDetail::getProduceDate, bo.getProduceDate());
        lqw.eq(bo.getExpireDate() != null, DrugExportDetail::getExpireDate, bo.getExpireDate());
        lqw.eq(bo.getPurchasePrice() != null, DrugExportDetail::getPurchasePrice, bo.getPurchasePrice());
        lqw.eq(bo.getRetailPrice() != null, DrugExportDetail::getRetailPrice, bo.getRetailPrice());
        lqw.eq(bo.getDiscount() != null, DrugExportDetail::getDiscount, bo.getDiscount());
        lqw.eq(bo.getQuantity() != null, DrugExportDetail::getQuantity, bo.getQuantity());
        lqw.eq(bo.getSplitQuantity() != null, DrugExportDetail::getSplitQuantity, bo.getSplitQuantity());
        lqw.eq(bo.getSurplusInventory() != null, DrugExportDetail::getSurplusInventory, bo.getSurplusInventory());
        lqw.eq(StringUtils.isNotBlank(bo.getImportMasterId()), DrugExportDetail::getImportMasterId, bo.getImportMasterId());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), DrugExportDetail::getLocation, bo.getLocation());
        lqw.eq(bo.getAllInventory() != null, DrugExportDetail::getAllInventory, bo.getAllInventory());
        lqw.eq(StringUtils.isNotBlank(bo.getRevokeType()), DrugExportDetail::getRevokeType, bo.getRevokeType());
        lqw.eq(bo.getProportion() != null, DrugExportDetail::getProportion, bo.getProportion());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), DrugExportDetail::getOrgId, bo.getOrgId());
        return lqw;
    }

    /**
     * 新增药品出库明细
     */
    @Override
    public Boolean insertByBo(DrugExportDetailBo bo) {
        DrugExportDetail add = BeanUtil.toBean(bo, DrugExportDetail.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改药品出库明细
     */
    @Override
    public Boolean updateByBo(DrugExportDetailBo bo) {
        DrugExportDetail update = BeanUtil.toBean(bo, DrugExportDetail.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DrugExportDetail entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药品出库明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
